package com.admin.common.shiro;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import com.alibaba.fastjson.JSONObject;
import com.admin.common.enums.EnumResult;
import com.admin.vo.SimpleResultVO;

/**
 * 校验用户登陆
 * 
 * @author ZY
 * @date 2018/10/18
 */
public class ShiroLoginFilter extends FormAuthenticationFilter {

    /**
     * 如未登陆，访问拒绝
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        SimpleResultVO result = new SimpleResultVO();
        result.setCode(EnumResult.USER_NOT_LOGIN.getCode());
        result.setMsg(EnumResult.USER_NOT_LOGIN.getMsg());
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            out = response.getWriter();
            out.write(JSONObject.toJSONString(result));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return false;
    }
}
